<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,100分)- 最长的密码（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>小王在进行游戏大闯关&#xff0c;有一个关卡需要输入一个密码才能通过&#xff0c;密码获得的条件如下&#xff1a;</p> 
<p>在一个密码本中&#xff0c;每一页都有一个由26个小写字母组成的若干位密码&#xff0c;每一页的密码不同&#xff0c;需要从这个密码本中寻找这样一个最长的密码&#xff0c;</p> 
<p>从它的末尾开始依次去掉一位得到的新密码也在密码本中存在。</p> 
<p>请输出符合要求的密码&#xff0c;如果有多个符合要求的密码&#xff0c;则返回<strong>字典序最大</strong>的密码。</p> 
<p>若没有符合要求的密码&#xff0c;则返回<strong>空字符串</strong>。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>密码本由一个字符串数组组成&#xff0c;不同元素之间使用空格隔开&#xff0c;每一个元素代表密码本每一页的密码。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>一个字符串</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">h he hel hell hello</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">hello</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">b ereddred bw bww bwwl bwwlm bwwln</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">bwwln</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题和<a href="https://blog.csdn.net/qfc_128220/article/details/128078962?spm&#61;1001.2014.3001.5501" title="华为机试 - 真正的密码_伏城之外的博客-CSDN博客">华为机试 - 真正的密码_伏城之外的博客-CSDN博客</a></p> 
<p>含义相同&#xff0c;解法类似。</p> 
<p></p> 
<p>根据考友反馈&#xff0c;本题支持单字符为符合要求的密码。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; line.split(&#34; &#34;);

  console.log(getResult(arr));
});

function getResult(arr) {
  arr.sort((a, b) &#61;&gt;
    a.length !&#61; b.length ? a.length - b.length : a &#61;&#61; b ? 0 : a &gt; b ? 1 : -1
  );

  const set &#61; new Set(arr);

  let ans &#61; &#34;&#34;;

  while (arr.length) {
    const str &#61; arr.pop();

    let end &#61; str.length - 1;

    if (end &#61;&#61;&#61; 0 &amp;&amp; ans &#61;&#61;&#61; &#34;&#34;) {
      ans &#61; str;
      continue;
    }

    while (set.has(str.substring(0, end))) {
      if (end &#61;&#61;&#61; 1) {
        return str;
      } else {
        end--;
      }
    }
  }

  return ans;
}
</code></pre> 
<p>优化逻辑</p> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; line.split(&#34; &#34;);

  console.log(getResult(arr));
});

function getResult(arr) {
  arr.sort((a, b) &#61;&gt;
    a.length !&#61; b.length ? a.length - b.length : a &#61;&#61; b ? 0 : a &gt; b ? 1 : -1
  );

  const set &#61; new Set(arr);

  while (arr.length) {
    const str &#61; arr.pop();

    let end &#61; str.length - 1;

    if (end &#61;&#61;&#61; 0) {
      return str;
    }

    while (set.has(str.substring(0, end))) {
      if (end &#61;&#61;&#61; 1) {
        return str;
      } else {
        end--;
      }
    }
  }

  return &#34;&#34;;
}
</code></pre> 
<p></p> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String[] arr &#61; sc.nextLine().split(&#34; &#34;);
    System.out.println(getResult(arr));
  }

  public static String getResult(String[] arr) {
    Arrays.sort(arr, (a, b) -&gt; a.length() !&#61; b.length() ? a.length() - b.length() : a.compareTo(b));

    LinkedList&lt;String&gt; link &#61; new LinkedList&lt;&gt;(Arrays.asList(arr));
    HashSet&lt;String&gt; set &#61; new HashSet&lt;&gt;(link);
	
	String ans &#61; &#34;&#34;;

    while (link.size() &gt; 0) {
      String str &#61; link.removeLast();
      int end &#61; str.length() - 1;
	  
	  if(end &#61;&#61; 0 &amp;&amp; &#34;&#34;.equals(ans)) {
		  ans &#61; str;
		  continue;
	  }

      while (set.contains(str.substring(0, end))) {
        if (end &#61;&#61; 1) {
          return str;
        } else {
          end--;
        }
      }
    }

    return ans;
  }
}</code></pre> 
<p>优化逻辑</p> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String[] arr &#61; sc.nextLine().split(&#34; &#34;);
    System.out.println(getResult(arr));
  }

  public static String getResult(String[] arr) {
    Arrays.sort(arr, (a, b) -&gt; a.length() !&#61; b.length() ? a.length() - b.length() : a.compareTo(b));

    LinkedList&lt;String&gt; link &#61; new LinkedList&lt;&gt;(Arrays.asList(arr));
    HashSet&lt;String&gt; set &#61; new HashSet&lt;&gt;(link);

    while (link.size() &gt; 0) {
      String str &#61; link.removeLast();
      int end &#61; str.length() - 1;

      if (end &#61;&#61; 0) {
        return str;
      }

      while (set.contains(str.substring(0, end))) {
        if (end &#61;&#61; 1) {
          return str;
        } else {
          end--;
        }
      }
    }

    return &#34;&#34;;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; input().split()


# 算法入口
def getResult(arr):
    arr.sort(key&#61;lambda s: (len(s), [ord(c) for c in s]))
    setA &#61; set(arr)

    ans &#61; &#34;&#34;

    while len(arr) &gt; 0:
        s &#61; arr.pop()
        end &#61; len(s) - 1

        if end &#61;&#61; 0 and ans &#61;&#61; &#34;&#34;:
            ans &#61; s
            continue

        while s[:end] in setA:
            if end &#61;&#61; 1:
                return s
            else:
                end -&#61; 1

    return ans


# 算法调用
print(getResult(arr))
</code></pre> 
<p>优化逻辑</p> 
<pre><code class="language-python"># 输入获取
arr &#61; input().split()


# 算法入口
def getResult(arr):
    arr.sort(key&#61;lambda s: (len(s), [ord(c) for c in s]))
    setA &#61; set(arr)

    while len(arr) &gt; 0:
        s &#61; arr.pop()
        end &#61; len(s) - 1

        if end &#61;&#61; 0:
            return s

        while s[:end] in setA:
            if end &#61;&#61; 1:
                return s
            else:
                end -&#61; 1

    return &#34;&#34;


# 算法调用
print(getResult(arr))
</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>